/*  J
**  Date 2007-05-21 23:53:31 
**  File mem.c  v 1.0
**  Author Mucker
**  See Copyright Notice in J.h
**  
*/

#include "J.h"

#include "mem.h"


static *__realloc(void *ptr, size_t osize, size_t nsize)
{
	static size_t mem=0;
    if(!nsize){
        free(ptr);
        return NULL;
    }
	else {
		mem +=nsize;
		//if(mem > 6000) exit(1);
        return realloc(ptr, nsize);
	}
}

void *mem_realloc(void *ptr, size_t osize, size_t nsize)
{
    return __realloc(ptr, osize, nsize);
}

void mem_free(void *ptr)
{
	free(ptr);
}

void *_growvector(void *ptr, size_t  *osize, size_t ofsize, 
                  size_t limit,char *errormsg)
{
    size_t newsize;
    void *newptr;
    if(*osize +4 > limit){
        if(*osize > limit)
        newsize = limit;
    }
    else{
        newsize = (*osize)+4;
        if(*osize == 0) 
            newsize = 4;
    }
    newptr = mem_realloc( ptr, (*osize)*ofsize, newsize*ofsize);
    *osize = newsize;
    return newptr;
}